package jmine.tec.report.domain;

import java.util.Collections;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.Version;

import jmine.tec.report.ColumnFactory;
import jmine.tec.report.ExecutableDataColumn;
import jmine.tec.report.ExecutableDataColumnContainer;

/**
 * @author takeshi
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class PersistableDataColumn implements ColumnFactory, Comparable<PersistableDataColumn> {

    private String key;

    private Long id, version = 0L;

    private PersistableTableDescriptor owner;

    private int prioridade;

    /**
     */
    public PersistableDataColumn() {
    }

    /**
     * @param key a chave
     * @param owner o 'dono'
     */
    public PersistableDataColumn(String key, PersistableTableDescriptor owner) {
        super();
        this.key = key;
        this.owner = owner;
    }

    /**
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     * @param o o outro datacolumn
     * @return int
     */
    public int compareTo(final PersistableDataColumn o) {
        if (this.prioridade < o.getPrioridade()) {
            return -1;
        }
        if (this.prioridade > o.getPrioridade()) {
            return 1;
        }
        return 0;
    }

    /**
     * @return the key
     */
    @Column(name = "CHAVE")
    public String getKey() {
        return this.key;
    }

    /**
     * @param key the key to set
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue
    public Long getId() {
        return this.id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the version
     */
    @Version
    public Long getVersion() {
        return this.version;
    }

    /**
     * @param version the version to set
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    /**
     * {@inheritDoc}
     */
    public List<ExecutableDataColumn> buildColumns(ExecutableDataColumnContainer container) {
        return Collections.singletonList(container.findExecutableDataColumn(this.key));
    }

    /**
     * @return the owner
     */
    @ManyToOne
    public PersistableTableDescriptor getOwner() {
        return this.owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(PersistableTableDescriptor owner) {
        this.owner = owner;
    }

    /**
     * @return the prioridade
     */
    @Column(name = "PRIORIDADE")
    public int getPrioridade() {
        return this.prioridade;
    }

    /**
     * @param prioridade the prioridade to set
     */
    public void setPrioridade(final int prioridade) {
        this.prioridade = prioridade;
    }

}
